"""
encoding = 'utf-8'
@author : ffine
@time   : 2022-04-28  19:17
@IDEA   : PyCharm
@goal   : learning rand module
"""
import random
import string

print(random.random())  # 0.0 <= x < 1.0
print(random.randint(1, 10))  # 1 <= x <= 10
print(random.randrange(1, 10, 2))  # 1 <= x <= 10, step = 2
print(random.choice(['apple', 'pear', 'banana']))  # random choice
print(random.sample(range(100), 10))  # random sample
print(random.uniform(1, 10))  # 1 <= x <= 10 (float)

targetList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.shuffle(targetList)  # shuffle, 打乱顺序
print(targetList)


# 生成随机密码
def get_random_password():
	"""
	生成随机密码
	:param length: 密码长度
	:return:
	"""
	# 密码字符集
	chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
	nums = '0123456789'
	fh = '!@#$%^&*'
	# 生成随机密码
	password = []
	for i in range(3):
		# 随机选择字符
		index = random.randint(0, len(chars) - 1)
		password.append(chars[index])
		# 随机选择数字
		index = random.randint(0, len(nums) - 1)
		password.append(nums[index])
		# 随机选择特殊字符
		index = random.randint(0, len(fh) - 1)
		password.append(fh[index])
	# 打乱密码
	random.shuffle(password)
	return "".join(password)


# 生成随机密码,大小写,数字,特殊字符
def get_random_password_all():
	"""
	生成随机密码,大小写,数字,特殊字符
	:param length: 密码长度
	:return:
	"""
	# 密码字符集
	chars = 'abcdefghijklmnopqrstuvwxyz'
	UpperChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	nums = '0123456789'
	fh = '!?@#$%^&*'
	# 生成随机密码
	password = []
	for i in range(2):
		# 随机选择字符
		index = random.randint(0, len(chars) - 1)
		password.append(chars[index])
		# 随机选择大写字符
		index = random.randint(0, len(UpperChars) - 1)
		password.append(UpperChars[index])
		# 随机选择数字
		index = random.randint(0, len(nums) - 1)
		password.append(nums[index])
		# 随机选择特殊字符
		index = random.randint(0, len(fh) - 1)
		password.append(fh[index])
	# 打乱密码
	random.shuffle(password)
	return "".join(password)


def get_random_password_all_2(length=8):
	numcount = random.randint(1, length//2)
	charcount = random.randint(1, length-numcount-1)
	punctuationcount = length - numcount - charcount
	
	numlist = [random.choice(string.digits) for i in range(numcount)]
	charslist = [random.choice(string.ascii_letters) for i in range(charcount)]
	punctuationlist = [random.choice(string.punctuation) for i in range(punctuationcount)]
	
	password = numlist + charslist + punctuationlist
	random.shuffle(password)
	return "".join(password)



for _ in range(10):
	print(get_random_password())  # 生成9位随机密码
	
for _ in range(10):
	print(get_random_password_all())  # 生成8位随机密码
	
for _ in range(10):
	print(get_random_password_all_2(10))  # 生成10位随机密码